package com.qubuyer.business.auth.viewmodel

import androidx.lifecycle.MutableLiveData
import com.qubuyer.repository.entitys.UserEntity
import com.qubuyer.business.auth.model.AuthModel
import com.qubuyer.business.mine.viewmodel.UserViewModel
import com.qubuyer.core.anno.AutoInject
import com.qubuyer.core.framework.BaseViewModel
import com.qubuyer.core.http.RetrofitCall
import com.qubuyer.utils.SessionUtil

/**
 * Author: linwg
 * Time: 2021/3/11 0011
 * Description:
 */
class UpdatePhoneViewModel : UserViewModel() {

    val getCodeResult = MutableLiveData<Boolean>()
    val getNewMobileCodeResult = MutableLiveData<Boolean>()
    val result = MutableLiveData<Int>()

    fun getSmsCode() {
        userEntity.value?.mobile?.let { mobile ->
            RetrofitCall<String?>(lifecycleOwner, this) {
                api { authModel.verifySms(mobile) }
                success {
                    getCodeResult.value = true
                }
                fail { _, _, _ ->
                    getCodeResult.value = false
                }
            }
        }
    }

    fun getNewMobileSmsCode(phone: String) {
        RetrofitCall<String?>(lifecycleOwner, this) {
            api { authModel.verifySms(phone) }
            success {
                getNewMobileCodeResult.value = true
            }
            fail { _, _, _ ->
                getNewMobileCodeResult.value = false
            }
        }
    }

    fun verifySmsCode(code: String) {
        userEntity.value?.mobile?.let { mobile ->
            RetrofitCall<String?>(lifecycleOwner, this) {
                api { authModel.updatePhoneStepOne(mobile, code) }
                success {
                    result.value = 200
                }
            }
        }
    }

    fun verifyNewMobileSmsCode(phone: String, code: String) {
        RetrofitCall<String?>(lifecycleOwner, this) {
            api { authModel.updatePhoneStepTwo(phone, code) }
            success {
                result.value = 2000
            }
        }
    }
}